﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using IssueTrackerTest.IssueTrackerService;

namespace IssueTrackerTest
{
    /// <summary>
    /// Summary description for ProjectCustomTypeColumnTest
    /// </summary>
    [TestClass]
    public class ProjectCustomTypeColumnTest
    {
        
  
        [TestMethod]
        public void TestAddProjectCustomTypeColumn()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();
            
            IssueTrackerService.ProjectCustomTypeColumn entity = new IssueTrackerService.ProjectCustomTypeColumn();


            try
            {
                client.Open();
                List<IssueTrackerService.ProjectCustomType> projectCustomTypeList = client.ListProjectCustomTypes().ToList();

                if (projectCustomTypeList != null)
                {
                    IssueTrackerService.ProjectCustomType customType = projectCustomTypeList.First();

                    entity.ProjectCustomTypeId = customType.Id;
                    entity.Description = "column desc";
                    entity.ImageUrl = @"http://microsoft.com";
                    entity.CreatorId = "testuser";
                    entity.Name = "column name";

                    IssueTrackerService.ProjectCustomTypeColumn result = client.CreateProjectCustomTypeColumn(entity);

                    Assert.IsNotNull(result.Id);
                }
                else
                {
                    Assert.Fail("Project Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestListByRef()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            client.Open();

            List<IssueTrackerService.ProjectCustomType> customTypeList;

            try
            {

                customTypeList = client.ListProjectCustomTypes().ToList();

                if (customTypeList != null)
                {
                    List<ProjectCustomTypeColumn> childList = client.ListProjectCustomTypeColumnsByRef(customTypeList.First().Id).ToList();

                    Assert.IsNotNull(childList);

                    Assert.IsTrue(childList.Count > 0);
                }

            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }

        }

        [TestMethod]
        public void TestUpdateProjectCustomTypeColumn()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            List<IssueTrackerService.ProjectCustomTypeColumn> list;

            try
            {
                client.Open();
                list = client.ListProjectCustomTypeColumns().ToList();

                if (list != null)
                {
                    IssueTrackerService.ProjectCustomTypeColumn entity = list.First();

                    entity.Description = "modified desc";

                    IssueTrackerService.ProjectCustomTypeColumn result = client.UpdateProjectCustomTypeColumn(entity);

                    Assert.AreEqual("modified desc", result.Description);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestDeleteProjectCustomTypeColumn()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            int id;
            List<IssueTrackerService.ProjectCustomTypeColumn> list;

            try
            {
                client.Open();
                list = client.ListProjectCustomTypeColumns().ToList();

                if (list != null)
                {
                    IssueTrackerService.ProjectCustomTypeColumn entity = list.First();

                    id = entity.Id;

                    client.DeleteProjectCustomTypeColumn(entity);

                    var checkProjectCustomTypeColumn = client.GetProjectCustomTypeColumn(id);

                    Assert.IsNull(checkProjectCustomTypeColumn);
                }
                else
                {
                    Assert.Fail("ProjectCustomTypeColumn Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

   
    }
}
